package com.dragon.backstage.controller;

import com.dragon.backstage.domain.param.user.UserAddParam;
import com.dragon.backstage.domain.param.user.UserAuthRoleAddBatchParam;
import com.dragon.backstage.domain.param.user.UserAuthRoleParam;
import com.dragon.backstage.domain.param.user.UserChangeStatusParam;
import com.dragon.backstage.domain.param.user.UserEditParam;
import com.dragon.backstage.domain.param.user.UserParam;
import com.dragon.backstage.domain.vo.user.UserAuthRoleVo;
import com.dragon.backstage.domain.vo.user.UserDetailVo;
import com.dragon.backstage.domain.vo.user.UserVo;
import com.dragon.backstage.service.UserService;
import com.dragon.common.annotation.AuthPermission;
import com.dragon.common.annotation.UserOperatorLog;
import com.dragon.common.domain.vo.PaginationVo;
import com.dragon.common.enums.BusinessType;
import com.dragon.common.utils.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author dragon
 * @since 2024-07-31 17:42:19
 */
@RestController
@RequestMapping("/api/system/user")
@Tag(name = "用户模块接口")
public class UserController {

    @Resource
    private UserService userService;


    /**
     * 用户列表
     *
     * @param param 用户列表请求参数
     * @return 用户列表
     */
    @GetMapping("/list")
    @Operation(summary = "用户列表")
    @AuthPermission(value = "system.user.list")
    public AjaxResult<PaginationVo<UserVo>> list(UserParam param) {
        return userService.userList(param);
    }

    /**
     * 用户详情
     *
     * @param userId 用户ID
     * @return 用户详情
     */
    @GetMapping("/detail")
    @Operation(summary = "用户详情")
    @AuthPermission(value = "system.user.detail")
    public AjaxResult<UserDetailVo> detail(@RequestParam(name = "userId", required = false) Long userId) {
        return userService.userDetail(userId);
    }

    /**
     * 添加用户
     *
     * @param param 添加用户参数
     * @return 添加结果
     */
    @PostMapping("/add")
    @Operation(summary = "添加用户")
    @AuthPermission(value = "system.user.add")
    @UserOperatorLog(title = "用户管理", businessType = BusinessType.INSERT)
    public AjaxResult<Void> add(@RequestBody UserAddParam param) {
        return userService.addUser(param);
    }

    /**
     * 编辑用户
     *
     * @param param 编辑用户参数
     * @return 编辑结果
     */
    @PutMapping("/edit")
    @Operation(summary = "编辑用户")
    @AuthPermission(value = "system.user.edit")
    @UserOperatorLog(title = "用户管理", businessType = BusinessType.UPDATE)
    public AjaxResult<Void> edit(@RequestBody UserEditParam param) {
        return userService.editUser(param);
    }

    /**
     * 删除用户
     *
     * @param userId 用户ID
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除用户")
    @AuthPermission(value = "system.user.delete")
    @UserOperatorLog(title = "用户管理", businessType = BusinessType.DELETE)
    public AjaxResult<Void> delete(@RequestParam(value = "userId", required = false) Long userId) {
        return userService.deleteUser(userId);
    }

    /**
     * 更改用户状态
     *
     * @param param 请求参数
     * @return 更改结果
     */
    @PutMapping("/changeStatus")
    @Operation(summary = "更改用户状态")
    @AuthPermission(value = "system.user.change.status")
    @UserOperatorLog(title = "用户管理", businessType = BusinessType.USER_CHANGE_STATUS)
    public AjaxResult<Void> changeStatus(@RequestBody UserChangeStatusParam param) {
        return userService.changeStatus(param);
    }

    /**
     * 授权角色列表
     *
     * @param param 请求参数
     * @return 授权角色列表
     */
    @GetMapping("/authRoleList")
    @Operation(summary = "授权角色列表")
    @AuthPermission(value = "system.user.auth.role")
    public AjaxResult<PaginationVo<UserAuthRoleVo>> authRoleList(UserAuthRoleParam param) {
        return userService.authRoleList(param);
    }

    /**
     * 批量添加授权角色
     *
     * @param param 请求参数
     * @return 批量添加结果
     */
    @PostMapping("/addAuthRoleBatch")
    @Operation(summary = "批量添加授权角色")
    @AuthPermission(value = "system.user.auth.role.add.batch")
    @UserOperatorLog(title = "用户管理", businessType = BusinessType.INSERT)
    public AjaxResult<Void> addAuthRoleBatch(@RequestBody UserAuthRoleAddBatchParam param) {
        return userService.addAuthRoleBatch(param);
    }

}
